package cn.edu.sdjzu.xg2106.controller;

import cn.edu.sdjzu.exception.BysjException;
import cn.edu.sdjzu.xg2106.domain.Teacher;
import cn.edu.sdjzu.xg2106.service.TeacherService;
import cn.edu.sdjzu.xg2106.util.Condition;
import cn.edu.sdjzu.xg2106.util.ControllerHelper;
import cn.edu.sdjzu.xg2106.util.JSONUtil;
import cn.edu.sdjzu.xg2106.util.Pagination;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

@WebServlet("/teacherRead.ctrl")
public class TeacherReadCtrl extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        //创建JSON对象，以便向前端响应信息
        JSONObject message_jsonObj = new JSONObject();
        //设置响应字符编码为utf8
        /*response.setContentType("application/json;charset=UTF-8");*/
        //读取来自浏览器的请求信息，实际上是body中的JSON数据
        String req_jsonStr = JSONUtil.getJSON(request);
        //解析为JSONObject对象
        JSONObject req_jsonObj = JSON.parseObject(req_jsonStr);
        if (req_jsonObj == null){
            message_jsonObj.put("message","未收到请求信息");
            response.getWriter().println(message_jsonObj);
            return;
        }
        try {

            int id = req_jsonObj.getInteger("id");
            Teacher teacher = TeacherService.getInstance().find(id);
            message_jsonObj.put("record",teacher);
            //响应到前端
            response.getWriter().println(message_jsonObj);
        }catch (BysjException e){
            message_jsonObj.put("message",e.getMessage());
            response.getWriter().println(message_jsonObj);
            e.printStackTrace();

        }catch (SQLException e){
            message_jsonObj.put("message","数据库操作异常");
            response.getWriter().println(message_jsonObj);
            e.printStackTrace();
        }catch (Exception e){
            message_jsonObj.put("message","网络异常");
            response.getWriter().println(message_jsonObj);
        }

    }
    //条件查询，分页响应
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        //创建JSON对象，以便向前端响应信息
        JSONObject message_jsonObj = new JSONObject();
        //设置响应字符编码为utf8
        /*response.setContentType("application/json;charset=UTF-8");*/
        //读取来自浏览器的请求信息，实际上是body中的JSON数据
        String req_jsonStr = JSONUtil.getJSON(request);
        //解析为JSONObject对象
        JSONObject req_jsonObj = JSON.parseObject(req_jsonStr);
        if (req_jsonObj == null){
            message_jsonObj.put("message","未收到请求信息");
            response.getWriter().println(message_jsonObj);
            return;
        }
        try {
            //读取分页信息，形成分页对象
            Pagination pagination = ControllerHelper.getPagination(req_jsonObj);
            //读取键conditionList对应的值
            List<Condition> conditionList= ControllerHelper.getConditions(req_jsonObj);
            //分页、条件查询获得符合条件的教师
            Collection<Teacher> teachers = TeacherService.getInstance().findAll(conditionList,pagination);
            message_jsonObj.put("totalNum",pagination.getTotalNum());
            message_jsonObj.put("records",teachers);
            //响应到前端
            response.getWriter().println(message_jsonObj);
        }catch (BysjException e){
            message_jsonObj.put("message",e.getMessage());
            response.getWriter().println(message_jsonObj);
            e.printStackTrace();

        }catch (SQLException e){
            message_jsonObj.put("message","数据库操作异常");
            response.getWriter().println(message_jsonObj);
            e.printStackTrace();
        }catch (Exception e){
            message_jsonObj.put("message","网络异常");
            response.getWriter().println(message_jsonObj);
        }

    }
}
